package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupMemberDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupMemberInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupMemberVo;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupMemberService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupMemberDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupRelationDao;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupMemberManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupBusinessPush;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupMemberPush;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupRelationPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-21 12:46:31<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
public class GroupMemberServiceImpl
		extends CrudServiceImpl<GroupMemberDao, GroupMemberDto, GroupMemberInfo, GroupMemberVo>
		implements GroupMemberService {

	@Resource
	private GroupRelationDao groupRelationDAO;
	@Resource
	private GroupMemberManager groupMemberManager;
	@Resource
	private GroupBusinessPush groupBusinessPush;
	@Resource
	private GroupMemberPush groupMemberPush;
	@Resource
	private GroupRelationPush groupRelationPush;
	@Resource
	private UserDao userDao;

	/**
	 * 获取用户在各个群中的权限
	 * 
	 * @author yitun
	 * @param userId
	 * @return
	 */
	public List<GroupMemberVo> getGroupMemberListOfUserInGroupByUserId(Long userId) {
		List<GroupMemberVo> list = dao.listVO(
				R.param("userId", userId));
		return list;
	}

	public List<GroupMemberVo> getGroupMemberListByGroupId(Long groupId) {
		List<GroupMemberVo> list = dao.listVO(
				R.param("groupId", groupId));
		return list;
	}

	public GroupMemberVo getGroupMemberById(Long id) {
		GroupMemberVo data = dao.getVO(id);
		return data;
	}

	public GroupMemberVo getGroupMemberByGroupIdUserId(Long groupId, Long userId) {
		GroupMemberVo data = dao.getVOByGroupIdUserId(groupId, userId);
		return data;
	}

	/**
	 * 
	 * Date 2019-01-23 22:15:19<br>
	 * Description 修改群成员备注名
	 * 
	 * @author yitun
	 * @param handleUserId
	 * @param groupId
	 * @param userId
	 * @param remark
	 * @return
	 * @since 1.0.0
	 */
	public Info updateRemark(Long handleUserId, Long groupId, Long userId, String remark) {
		Info info = new Info();
		// 自己修改自己的群显示名称
		boolean isOwner = handleUserId.equals(userId);
		// 管理员或者群主
		boolean hasPermission = groupMemberManager.isAdmin(groupId, handleUserId) || groupMemberManager.isOwner(groupId, handleUserId);

		if (isOwner || hasPermission) {
			if (YitunStringUtil.isEmpty(remark))
				remark = userDao.get(userId).getNickname();

			int mark = dao.updateNickname(groupId, userId, remark);
			if (mark == 0) {
				info.addWarning(ExceptionEnum.DO_FAIL);
			} else {
				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupMemberPush.pushUpdate(groupId, userId, userIds);
			}
		} else {
			info.addWarning(ExceptionEnum.USER_HAVE_NO_PERMISSIONS);
		}
		return info;
	}

	/**
	 * 
	 * Date 2019-01-23 22:13:27<br>
	 * Description 设置用户权限
	 * 
	 * @author yitun
	 * @param handleUserId
	 * @param groupId
	 * @param userId
	 * @param position
	 * @return
	 * @since 1.0.0
	 */
	public Info updatePosition(Long handleUserId, Long groupId, Long userId, String position) {
		// 只能是群主才能设置权限
		Info info = new Info();
		// 自己
		boolean isOwner = handleUserId.equals(userId);
		// 群主
		boolean hasPermission = groupMemberManager.isOwner(groupId, handleUserId);
		if (!isOwner && hasPermission) {
			int mark = dao.updatePosition(groupId, userId, position);
			if (mark == 0 ) {
				info.addWarning(ExceptionEnum.DO_FAIL);
			} else {
				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupMemberPush.pushUpdatePosition(groupId, userId, position, userIds);
			}
		} else if (isOwner) {
			info.addWarning(ExceptionEnum.SOCK_GROUP_MEMBER_CANT_SET_SELF);
		} else if (!hasPermission) {
			info.addWarning(ExceptionEnum.USER_HAVE_NO_PERMISSIONS);
		}
		return info;
	}

	/**
	 * 
	 * Date 2019-01-23 22:12:12<br>
	 * Description 删除群成员
	 * 
	 * @author yitun
	 * @param handleUserId
	 * @param groupId
	 * @param userId
	 * @return
	 * @since 1.0.0
	 */
	public Info delete(Long handleUserId, Long groupId, Long userId) {

		// 自己不能删除自己，需要用退群的方式
		Info info = new Info();
		// 自己
		boolean isOwner = handleUserId.equals(userId);
		// 管理员或者群主
		boolean hasPermission = groupMemberManager.isAdmin(groupId, userId) || groupMemberManager.isOwner(groupId, userId);

		if (!isOwner && hasPermission) {
			int mark = dao.deleteByGroupIdUserId(groupId, userId);
			groupRelationDAO.deleteGroupCategoryMember(userId, groupId);

			if (mark == 0) {
				info.addWarning(ExceptionEnum.DO_FAIL);
			} else {
				// TODO 推送信息

				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupMemberPush.pushDelete(groupId, userId, userIds);

				groupRelationPush.pushDelete(IdGen.getNumber(), groupId, userId);
			}
		} else if (isOwner) {
			info.addWarning(ExceptionEnum.USER_HAVE_NO_PERMISSIONS);
		} else if (!hasPermission) {
			info.addWarning(ExceptionEnum.USER_HAVE_NO_PERMISSIONS);
		}
		return info;
	}

	/**
	 * 
	 * Date 2019-01-27 11:08:11<br>
	 * Description 退出
	 * 
	 * @author yitun
	 * @param groupId
	 * @param userId
	 * @return
	 * @since 1.0.0
	 */
	public Info quit(Long groupId, Long userId) {

		Info info = new Info();
		// 群主不可以退群
		boolean isOwner = groupMemberManager.isOwner(groupId, userId);

		if (!isOwner) {
			int mark = dao.deleteByGroupIdUserId(groupId, userId);
			groupRelationDAO.deleteGroupCategoryMember(userId, groupId);

			if (mark == 0) {
				info.addWarning(ExceptionEnum.SOCK_GROUP_MEMBER_REMOVE_FAIL);
			} else {
				// TODO 推送信息
				List<Long> aoIds = groupMemberManager.getGroupAdminAndOwnerUserIdList(groupId);
				groupBusinessPush.pushQuit(IdGen.getNumber(), groupId, userId, aoIds);

				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupMemberPush.pushDelete(groupId, userId, userIds);

				groupRelationPush.pushDelete(IdGen.getNumber(), groupId, userId);
			}
		} else if (isOwner) {
			info.addWarning(ExceptionEnum.SOCK_GROUP_MEMBER_OWNER_REMOVE_FAIL);
		}
		return info;
	}
}
